//
//  GroupViewController.swift
//  HoneyBee
//
//  Created by point on 2018/1/24.
//  Copyright © 2018年 point. All rights reserved.
//

import UIKit
import RxCocoa
import RxSwift
import MJRefresh

enum GroupListType {
    case All    //所有小组
    case My     //我创建的小组
    case Joined //我加入的小组
}

private let kCellID = "kTopicGroupCellId"


class GroupViewController: UIViewController {
    
    @IBOutlet weak var tableView: UITableView!
    
    var groupListType : GroupListType = .All
    var pageNo : Int = 1
    
    private var groupList:[TopicGroupModel] = [TopicGroupModel]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
        
        // Do any additional setup after loading the view.
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    public func loadData(userId:String, isNew:Bool){
        
        self.hudLoad()
        
        let dict:Dictionary = [
            "timestamp":"2",
            "longitude": "122",
            "latitude":"23",
            "pageNo":String(format: "%d", pageNo),
            "pageSize":"10",
            "sign":"2"
            ] as [String : Any]
        
        let str = dict.formatApi() + "&operatorId=" + String(UserInfo.shareInstance.userModel().id)
        NetworkTools.shareInstance.loadData(MethodType.post, URLString: kqueryGroupList+str, parameters: dict) { (data, isResult) in
            if !isResult {
                guard let resultDict = data as? [String : NSObject] else { return }
                guard let dataArray = resultDict["data"] as? [NSObject] else { return }
                let groupModels = TopicGroupModel.tmSpeelBreakModelArr(dataArray)
                self.groupList = (groupModels as? [TopicGroupModel])!
                
                self.tableView.reloadData()
            }
            
            self.tableView.mj_header.endRefreshing()
            self.tableView.mj_footer.endRefreshing()
            
            self.hudHide()
            
        }
        
    }
    
}
extension GroupViewController:UITableViewDelegate,UITableViewDataSource {
    func numberOfSections(in tableView: UITableView) -> Int {
        return 1
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return self.groupList.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: kCellID, for: indexPath) as! TopicMyGroupTableViewCell
        cell.configModel(model: self.groupList[indexPath.item], index: indexPath)
        
        return cell
    }
    
    func tableView(_ tableView: UITableView, estimatedHeightForRowAt indexPath: IndexPath) -> CGFloat {
        return 100
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: false)
        
        let model = self.groupList[indexPath.item]
        let vc = GroupTopicViewController()
        vc.configModel(model: model);
        self.navigationController?.pushViewController(vc, animated: true)
    }
    
}


extension GroupViewController {
    fileprivate func setupUI(){
        
        tableView.register(UINib(nibName: "TopicMyGroupTableViewCell", bundle: nil), forCellReuseIdentifier: kCellID)
        tableView.separatorStyle = .singleLine
        tableView.delegate = self
        tableView.dataSource = self
        tableView.tableFooterView = UIView(frame: CGRect.zero)
        tableView.separatorStyle = .none
        tableView.backgroundColor = UIColor(hex: "#eeeff0")
        tableView.estimatedRowHeight = 50
        
        tableView.mj_header = MJRefreshNormalHeader(refreshingBlock: {
            self.loadData(userId: "1", isNew: true)
        })
        tableView.mj_footer = MJRefreshAutoNormalFooter(refreshingBlock: {
            self.loadData(userId: "1", isNew: false)
        })
        tableView.mj_header.beginRefreshing()
        
    }
    
}
